/**
 * @description  : 电子送达表单
 * @descriptionDetail : 电子送达表单
 * @copyright    : 浙江烟草
 * @author       : mc
 * @create       : 2022-12-05 16:25:20
 */
<template>
  <view class="page">
    <theme-navbar :is-white="false" title="电子送达" style="z-index: 999">
    </theme-navbar>
    <view class="content">
      <u-form :model="eleDeliverForm" ref="uEleForm" label-position="top" label-width="auto" :error-type="['border-bottom','message']">
        <u-form-item label="电子送达文书名称">
          {{ docInfo.docName }}
        </u-form-item>
        <u-form-item label="电子送达文书内容" prop="eleInfoTemplate" :required="true">
          <u-input type="textarea" v-model="eleDeliverForm.eleInfoTemplate" placeholder="请输入"></u-input>
        </u-form-item>
        <u-form-item :border-bottom="false" label="电子送达方式" label-position="left" :required="true" prop="decisionWay">
        </u-form-item>
        <u-form-item label-position="left" :label="itm.partyName" v-for="(itm, idx) in deliverItemEleList" :key="'fi'+idx">
          <u-radio-group v-model="itm.check" @change="radioGroupChange($event, idx)">
            <u-radio 
              v-for="(ipx, index) in itm.list" 
              :key="'ip'+index"
              :name="ipx.bcbCode"
            >
              {{ ipx.bcbName }}
            </u-radio>
          </u-radio-group>
        </u-form-item>
      </u-form>
    </view>
    <view class="popup-submit-btns btn-other-cls">
      <u-button class="u-btn" type="primary" @click="submitBtn()">
        提交
      </u-button>
    </view>
  </view>
</template>

<script>
import caseService from "@/service/case/case.service.js";
export default{
  name:"EleDeliverComponent",
  props:{
    deliverEleList:{
      type: Array,
      default: () => [],
    },
    docInfo:{
      type: Object,
      default: () => {},
    },
  },
  data(){
    return {
      deliverItemEleList:[],
      eleDeliverForm:{
        decisionWay:[],
      },
      rules:{
        eleInfoTemplate:[
          { 
            required: true, 
            message: '请输入送达文书内容', 
            // 可以单个或者同时写两个触发验证方式 
            trigger: ['change','blur',],
          },
        ],
        decisionWay:[
          { 
            validator: (rule, value, callback) => {
              return this.$u.test.array(this.eleDeliverForm.decisionWay) && this.eleDeliverForm.decisionWay.length == this.deliverItemEleList.length;
            },
            message: '请选择送达方式', 
            // 可以单个或者同时写两个触发验证方式 
            trigger: ['change','blur',],
          },
        ],
      },
    }
  },
  mounted() {
    this.$refs.uEleForm.setRules(this.rules);
  },
  created() {
    this.init();
  },
  methods:{
    init(){
      this.eleDeliverForm.docInsUuid = this.docInfo.docInsUuid || '';
      let dws = this.$u.dic.getDicByKeyCode(this.$u.dic.DIC_KEYS.S_DELIVERY_TYPE);
      this.deliverItemEleList = [];
      let tmpDeliverList = [];
      for (let i = 0; i < this.deliverEleList.length; i++) {
        tmpDeliverList = [];
        let tmp = this.deliverEleList[i];
        if(!this.eleDeliverForm.bizUuid){
          this.eleDeliverForm.bizUuid = tmp.caseUuid;
        }
        tmp.decisionWay = tmp.decisionWay || '';
        for (let j = 0; j < dws.length; j++) {
          if(tmp.decisionWay.indexOf(dws[j].bcbCode) > -1){
            tmpDeliverList.push(dws[j]);
          }
        }
        this.deliverItemEleList.push({
          list: this.$u.deepClone(tmpDeliverList),
          partyName: tmp.partyName,
          confirmId: tmp.confirmId,
          check:tmpDeliverList.length > 0 ? tmpDeliverList[0].bcbCode : "",
        });
      }
      this.updateDecisionWay();
    },
    updateDecisionWay(){
      this.eleDeliverForm.decisionWay = [];
      this.deliverItemEleList.forEach(il => {
        if(!this.$u.test.isEmpty(il.check)){
          this.eleDeliverForm.decisionWay.push(il.check);
        }
      });
    },
    submitBtn(){
      this.$refs.uEleForm.validate(valid => {
        if (valid) {
          this.$modal.showLoading('正在提交...');
          this.eleDeliverForm.list = this.deliverItemEleList.map(itm => {
            return {
              confirmId: itm.confirmId,
              decisionWay: itm.check,
            };
          });
          caseService.eleDeliverInfoSend(this.eleDeliverForm).then(res => {
            this.$modal.hideLoading();
            if(res.success){
              this.$emit("close", true);
            }else{
              this.$u.toast(res.msg);
            }
          }).catch(e => {
            this.$modal.hideLoading();
            console.log(e);
            this.$u.toast('发送失败:' + e);
          });
        }else{
          console.log("22222");
        }
      });
    },
    radioGroupChange(e, idx){
      console.log(e, idx);
      this.deliverItemEleList[idx].check = e;
      this.updateDecisionWay();
    },
  },
}
</script>

<style scoped lang="scss">
	.page{
		padding: 0;
	}
	.content{
		padding:$cm-conent-padding-column $cm-content-paddng-row 160rpx;
	}
	.btn-other-cls{
		margin-left: $cm-content-paddng-row;
		width: calc(100% - 34px);
		background-color: transparent !important
	}
	.right-icon{
		margin-right: 20rpx;
		font-size: 28rpx;
		color: white;
	}
	/deep/.u-radio-group{
		width: 100%;
		justify-content: flex-start;
	}
</style>